<?php
/**
 * Created by PhpStorm.
 * User: lxy
 * Date: 2016/3/9
 * Time: 9:52
 */

namespace app\models;

use Yii;
use app\lib\BaseModel;
use app\lib\WeixinUtils;
class FxDao extends BaseModel
{
    //获取用户的分销码
    public static function get_INVITE_CODE($user_id)
    {
        $sql = "select id from T_WX_FX_USER_INVITE_CODE where  USER_ID=:user_id";
        $cmd = static::query()->createCommand($sql);
        $cmd->bindValue(':user_id', $user_id);

        $id = $cmd->queryScalar();
        if ($id == false) {
            $sql = "insert into T_WX_FX_USER_INVITE_CODE (id,USER_ID) values (user_invite_code.nextval,:user_id)";

            $cmd = static::query()->createCommand($sql);
            $cmd->bindValue(':user_id', $user_id);

            $re = $cmd->execute();
            if ($re == 0) {
                throw     new \Exception("插入T_WX_FX_USER_INVITE_CODE数据失败");
            }
            $sql = "select id from T_WX_FX_USER_INVITE_CODE where  USER_ID=:user_id";
            $cmd = static::query()->createCommand($sql);
            $cmd->bindValue(':user_id', $user_id);

            $id= $cmd->queryScalar();

            static::update_INVITE_CODE_QR($id);

        }
        return $id;



    }

//通过分销码查询分销码表
    public static function get_T_WX_FX_USER_INVITE_CODE($id)
    {
        $sql = "select trunc(SYSDATE)-trunc(to_date(c.QR_CREATE_DATETIME,'yyyy-mm-dd hh24:mi:ss')) as days,user_id
        ,u.nick_name,u.header_url,c.QR_TICKET from T_WX_FX_USER_INVITE_CODE c,t_Wxmall_User u where c.id=:id
        and u.id=c.user_id ";
        $cmd = static::query()->createCommand($sql);
        $cmd->bindValue(':id', $id);

        return $cmd->queryAll();
    }
    //更新分销码表qr
    public static function update_INVITE_CODE_QR($id)
    {
        //获取qr
        $t=WeixinUtils::qrcode_temp($id);
        $QR_TICKET=urlencode($t->ticket);
        //更新数据库
        $sql = "update T_WX_FX_USER_INVITE_CODE set   QR_CREATE_DATETIME=TO_CHAR(SYSDATE,'yyyy-mm-dd hh24:mi:ss'),QR_TICKET=:QR_TICKET where id=:id";
        $cmd = static::query()->createCommand($sql);
        $cmd->bindValue(':id', $id);
        $cmd->bindValue(':QR_TICKET', $QR_TICKET);

        $re = $cmd->execute();
        if ($re == 0) {
            throw     new \Exception("更新T_WX_FX_USER_INVITE_CODE数据失败");
        }
        return $QR_TICKET;
    }


/*
CREATE OR REPLACE PROCEDURE WEIXINTEST.P_WXMALL_USER_INVITE
(
ID IN NUMBER --邀请码ID
, USER_ID IN VARCHAR2 --下线user_Id
, RESULTS OUT VARCHAR2 --返回1 成功 其他为错误信息
) AS*/
    public static function P_WXMALL_USER_INVITE($id,$user_id){

        $cmd=  static::query()->createCommand('call P_WXMALL_USER_INVITE(:ID,:USER_ID,:RESULTS)');
        $cmd->bindValue(':ID',(string)$id);//传入的有可能是object  ，不转换会报错
        $cmd->bindValue(':USER_ID',$user_id);


        $cmd->bindParam(':RESULTS', $RESULTS,\PDO::PARAM_STR,50);

        $cmd->execute();

        return $RESULTS;
    }

    //获取我的团队
    public static function get_TeamList($start=0,$pagesize=6,$user_id){
        $cSql='select u.NICK_NAME,u.HEADER_URL,t.CREATE_DATETIME from T_WX_FX_TEAM t,t_wxmall_user u where t.p_user_id=:user_id and u.id=t.c_user_id
               order by t.CREATE_DATETIME desc';
        $cmd= static::query()->createCommand(static::pageSql($cSql,$start,$pagesize));
        $cmd->bindValue(':user_id',$user_id);
        return   $cmd->queryAll();
    }
    //获取我的团队
    public static function get_TeamListCount($user_id){
        $cSql='select count(1) from T_WX_FX_TEAM where p_user_id=:user_id';
        $cmd= static::query()->createCommand($cSql);
        $cmd->bindValue(':user_id',$user_id);
        return   $cmd->queryScalar();
    }

    //获取当前我的信息和分销比率
    public static function get_CurrentIndex($user_id){
        $cSql='select d.discount,d.enjoy,u.* from T_WXMALL_USER u,t_wxmall_discount d where
          u.id=:user_id and  u.buy_result >=d.buylower and u.buy_result <d.buyupper';
        $cmd= static::query()->createCommand($cSql);
        $cmd->bindValue(':user_id',$user_id);
        return   $cmd->queryAll();
    }

    //获取我的业绩列表
    public static function get_MyResultList($start=0,$pagesize=6,$user_id){
        $cSql='select r.*,u.nick_name from ((select t.*,o.user_id b_user_id from T_WX_USER_BUY_RESULT_RECORD t,t_wxmall_order o
            where  t.user_id=:user_id and record_type=1 and o.id=t.record_id )
            union
            (select t.*,o.user_id b_user_id from T_WX_USER_BUY_RESULT_RECORD t,t_wxmall_dorabag_order o
            where t.user_id=:user_id and record_type=2 and o.code=t.record_id
            ))
            r,t_wxmall_user u where u.id=r.b_user_id   order by r.CREATE_DATETIME desc';
        $cmd= static::query()->createCommand(static::pageSql($cSql,$start,$pagesize));
        $cmd->bindValue(':user_id',$user_id);
        return   $cmd->queryAll();
    }
    //获取我的现金变化列表
    public static function get_MyBalanceList($start=0,$pagesize=6,$user_id){
        $cSql='select * from t_wx_user_cash_record where user_id=:user_id order by CREATE_DATETIME desc';
        $cmd= static::query()->createCommand(static::pageSql($cSql,$start,$pagesize));
        $cmd->bindValue(':user_id',$user_id);
        return   $cmd->queryAll();
    }
    //获取现金变化 详情
    public static function get_user_buy_result_record($rid,$user_id){

        $cSql='select * from t_wx_user_cash_record where user_id=:user_id and id=:rid';
        $cmd= static::query()->createCommand($cSql);
        $cmd->bindValue(':user_id',$user_id);
        $cmd->bindValue(':rid',$rid);
        return   $cmd->queryAll();
    }
    //获取提现申请
    public static function get_fx_get_cash_apply($rid,$user_id){

        $cSql='select * from t_wx_fx_get_cash_apply where  user_id=:user_id and user_cash_record_id=:rid';
        $cmd= static::query()->createCommand($cSql);
        $cmd->bindValue(':user_id',$user_id);
        $cmd->bindValue(':rid',$rid);
        return   $cmd->queryAll();
    }
/*提交提现申请
 create or replace PROCEDURE P_WXMALL_CASHAPPLY
(
  user_Id IN VARCHAR2   --用户Id
, cash IN NUMBER        --提现金额
, cash_record_Id OUT VARCHAR2  --现金变化表Id
, RESULTS OUT VARCHAR2
)
)*/
    public static function P_WXMALL_CASHAPPLY($user_id,$cash){

        $cmd=  static::query()->createCommand('call P_WXMALL_CASHAPPLY(:USER_ID,:cash,:cash_record_Id,:RESULTS)');
        $cmd->bindValue(':USER_ID',$user_id);
        $cmd->bindValue(':cash',$cash);


        $cmd->bindParam(':cash_record_Id', $cash_record_Id,\PDO::PARAM_STR,50);
        $cmd->bindParam(':RESULTS', $RESULTS,\PDO::PARAM_STR,50);

        $cmd->execute();

        return [$RESULTS,$cash_record_Id];
    }
    //获取当前现金可用余额
    public static function get_CurrentCash($user_id){
        $cSql='select  cash_account from T_WXMALL_USER where id=:user_id ';
        $cmd= static::query()->createCommand($cSql);
        $cmd->bindValue(':user_id',$user_id);
        return   $cmd->queryScalar();
    }
    //曲线图数据
    public static function get_T_WX_USER_CASHINCOME($user_id,$pagesize){

        $cSql='select * from T_WX_USER_CASHINCOME where  user_id=:user_id  order by create_date desc  ';
        $cmd= static::query()->createCommand(static::pageSql($cSql,0,$pagesize) );
        $cmd->bindValue(':user_id',$user_id);

        return   $cmd->queryAll();
    }

    //分销排名 1
    public static function get_rank1_list($start,$pagesize){
        $cSql='select r.*,r.pay_result /100 val from t_wxmall_user r where pay_result>0 order by pay_result desc,id ';
        $cmd= static::query()->createCommand(static::pageSql($cSql,$start,$pagesize) );
        return   $cmd->queryAll();
    }
    public static function get_rank1_my($val){
        $cSql=' select count(1) from t_wxmall_user r where pay_result>:val';
        $cmd= static::query()->createCommand($cSql);
        $cmd->bindValue(':val',$val);
        $mytopcount= $cmd->queryScalar();

        $cSql='select * from (select r.pay_result from t_wxmall_user r where pay_result>:val
         order by pay_result  ,id )xx where rownum<=1';
        $cmd= static::query()->createCommand($cSql);
        $cmd->bindValue(':val',$val);
        $mytopVal= $cmd->queryScalar();
        return ['mytopcount'=>$mytopcount,'mytopVal'=>$mytopVal];
    }
    //分销排名 2
    public static function get_rank2_list($start,$pagesize){
        $cSql='select r.*,r.fx_user_count val from t_wxmall_user r where fx_user_count>0 order by fx_user_count desc,id ';
        $cmd= static::query()->createCommand(static::pageSql($cSql,$start,$pagesize) );
        return   $cmd->queryAll();
    }
    public static function get_rank2_my($val){
        $cSql=' select count(1) from t_wxmall_user r where fx_user_count>:val';
        $cmd= static::query()->createCommand($cSql);
        $cmd->bindValue(':val',$val);
        $mytopcount= $cmd->queryScalar();

        $cSql='select * from (select r.fx_user_count from t_wxmall_user r where fx_user_count>:val
         order by fx_user_count  ,id )xx where rownum<=1';
        $cmd= static::query()->createCommand($cSql);
        $cmd->bindValue(':val',$val);
        $mytopVal= $cmd->queryScalar();
        return ['mytopcount'=>$mytopcount,'mytopVal'=>$mytopVal];
    }
}